package com.cssw.fyzb.busi.api.live;

import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.busi.service.acct.AcctService;
import com.cssw.fyzb.busi.service.live.GiftService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.busi.util.BizUtil;
import com.cssw.fyzb.core.model.dodb.CmfGift;
import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.simplelock.SimpleLock;
import com.ydn.simplelock.SimpleLockFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 赠送礼物
 * @author wondream
 */
@Function(description = "主播收到礼物统计(根据礼物id)", parameters = {
        @Parameter(name = "fromUserId", type = Type.String, description = "赠送礼物用户ID", required = true),
        @Parameter(name = "toUserId", type = Type.String, description = "收到礼物用户ID", required = true),
        @Parameter(name = "liveId", type = Type.String, description = "直播ID", required = true),
        @Parameter(name = "giftId", type = Type.String, description = "礼物ID", required = true)
})
@Component
public class SendGift implements Action {
    public static final Logger logger = LoggerFactory.getLogger(SendGift.class);

    @Autowired
    GiftService giftService;

    @Autowired
    private SimpleLockFactory simpleLockFactory;

    @Override
    public Response execute(Request request) throws Exception {
        Long fromUserId = request.getLong("fromUserId");
        Long toUserId = request.getLong("toUserId");
        Long liveId = request.getLong("liveId");
        Long giftId = request.getLong("giftId");

        if (fromUserId.longValue() == toUserId.longValue()){
            return Response.fail("不能送给自己");
        }

        SimpleLock fromLock = simpleLockFactory.getLock("USER_" + fromUserId);
        SimpleLock toLock = simpleLockFactory.getLock("USER_" + toUserId);
        try {
            fromLock.acquire();
            toLock.acquire();

            giftService.sendGift(fromUserId,  toUserId, liveId, giftId);
            return Response.success("赠送成功");
        }catch (Exception e){
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.error("", e);
            return Response.fail("赠送失败");
        }
        finally {
            try{
                fromLock.release();
            } catch(Exception e){
                logger.error("", e);
            }
            try{
                toLock.release();
            } catch(Exception e){
                logger.error("", e);
            }
        }
    }
}
